<% session.putValue("mysJDBCDriver", "com.mysql.jdbc.Driver"); 
session.putValue("mysURL", "jdbc:mysql://localhost:3306/snazzy_bids");  
session.putValue("mysUserID" , "root"); 
session.putValue("mysPassword", ""); 
%>

<%
	if((request.getParameter("action")!=null)&&	(request.getParameter("action").trim().equals("logout")))
	{
		session.putValue("login","");
		response.sendRedirect("/");
		return;
	}
		String username = request.getParameter("username");
     	
    	
	session.putValue("login","");
	if ((username!=null))
	{
		if (username.trim().equals(""))
		{
			response.sendRedirect("index.htm");
		}
		else
		{
			// code start here
			java.sql.Connection conn=null;
			try {
		            	Class.forName((String)session.getValue("mysJDBCDriver")).newInstance();
            			java.util.Properties sysprops=System.getProperties();
            			sysprops.put("user", (String)session.getValue("mysUserID"));
            			sysprops.put("password", (String)session.getValue("mysPassword"));
        
				//connect to the database
            			conn=java.sql.DriverManager.getConnection((String)session.getValue("mysURL"),sysprops);
            			System.out.println("Connected successfully to database using JConnect");
            
            			conn.setAutoCommit(false);
            			java.sql.Statement stmt1=conn.createStatement();
				java.sql.ResultSet rs = stmt1.executeQuery(" select * from Customer where UserName='"+username+"' ");
				if (rs.next())
				{
					// login success
					session.putValue("login",username);
					session.putValue("status", "customer");
					response.sendRedirect("CustomerHomePage.jsp");
				}
				else
				{
					rs = stmt1.executeQuery(" select * from Employee where SSN='"+username+"' ");
					if(rs.next())
					{
						if (rs.getString(5).equalsIgnoreCase("Manager"))
						{
							session.putValue("status", "manager");
							session.putValue("login", username);
							response.sendRedirect("ManagerHomePage.jsp");
						}
						session.putValue("login", username);
						session.putValue("status", "employee");
						response.sendRedirect("EmployeeHomePage.jsp");
					}
						
					else
					{
						// username or password mistake
						response.sendRedirect("passMistake.jsp");
					}
				}
			} catch(Exception e)
			{
				e.printStackTrace();
			}
			finally{
				try{conn.close();}catch(Exception ee){};
			}
		}
	}
%>